<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Hash;
use DB;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\HomeLoginPostRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class LoginController extends Controller
{
    //显示前台登录页面
    public function getLogin()
    {
        //显示模板
        return view('home.login.login');
    }

    //前台登录验证
    public function postCheck(HomeLoginPostRequest $request)
    {
        // dd($request->all());
        //检测用户是否存在
        $user = '';
        $user = DB::table('hs_member')->where('name','=',$request->input('username'))->first();
        
        // dd($user);
        //检测密码经过哈希加密后是否一致
        if($user){
            if(Hash::check($request->input('password'),$user->password)){
                //登录成功
                session(['id'=>$user->id]);
                //跳转到前台首页
                return redirect('/home/personage');
            }else{
                //失败后
                return back()->with('error','用户名或密码错误');
            }
        }else{
            return back()->with('error','用户名不存在');
        }    
    }

    //前台退出
    public function getLogout()
    {
        session()->forget('id');
        return redirect('/');
    }

    //前台注册页面显示
    public function register()
    {
        //解析模板 前台会员注册
        // return '2222';
        return view('home.register.register');
    }

    //执行注册
    public function doregister(Request $request)
    {
        // dd($request->all());
        //数据验证
        $this->validate($request, [
            'name' => 'required|unique:hs_member|max:18|min:6',
            'password' => 'required|max:18|min:6',
            'repassword' => 'required|same:password',
            'email' => 'required|email',
        ],[
            'name.required'=>'用户名不能为空',
            'name.unique'=>'用户名已存在,请重新填写',
            'name.min'=>'用户名长度不够,最小6位',
            'name.max'=>'用户名长度太长,最多18位',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不够,最小6位',
            'password.max'=>'密码长度太长,最多18位',
            'repassword.required'=>'确认密码不能为空',
            'repassword.same'=>'确认密码和密码填写的不一样',
            'email.required'=>'邮箱不能为空',
            'email.email'=>'邮箱格式不正确',
        ]);
        // dd($request->all());

        //检测验证码是否正确
        if($request->input('vcode') != session('Vcode')){
            return back()->with('error','验证码不正确');
        }

        //处理数据
        $data = $request->only(['email','password']);
        //密码加密
        $data['password'] = Hash::make($data['password']);
        $data['name'] = $request->input('name');
        $data['token'] = str_random(50);//随机50个字符
        $data['status'] = 1;//状态 1未激活 0 激活
        $data['zh_id'] = rand(100000000,999999999);//生成一个随机的9位的账户
        // dd($data);

        //执行数据插入 获取插入后的id
        $id = DB::table('hs_member')->insertGetId($data);
        if($id){
            $this->sendMail($id,$data['token'],$data['email']);
            return view('home.login.login')->with('success','您已注册成功,激活后,请登录');
        }else{
            return back()->with('error','注册失败,请与管理员联系');
        }

    }

    //显示验证码
    public function vcode()
    {
        ob_clean();//清除输出缓存区的内容
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
        //看到图片输出 需要die
        // die;
    }

    //邮箱激活
    public function sendMail($id,$token,$email)
    {
        //发送原始字段形式的邮件
        // Mail::raw('传奇网欢迎你注册请点击.即可获取大量资源...', function ($message) {
        //     //设置邮件标题
        //     $message->subject('激活邮件');
        //     //发送给谁
        //     $message->to('13520399583@163.com');
        // });

        //发送其它形式的邮件
        Mail::send('mail.re', ['id'=>$id,'token'=>$token], function ($message)use($email) {
            $message->to($email)->subject('激活邮件');
        });

        // return view('mail.re');
    }

    //激活用户信息
    public function jihuo(Request $request)
    {
        $id = $request->input('id');
        //根据id查询用户
        $user = DB::table('hs_member')->where('id',$id)->first();
        //请求是否正确
        if($user->token == $request->input('token')){
            $data['status'] = 0;
            $data['token'] = str_random(50);
            $res = DB::table('hs_member')->where('id',$id)->update($data);
            if($res){
                return redirect('/home/login/login')->with('success','注册成功');
            }else{
                return redirect('/home/register/register')->with('error','注册失败');
            }
        }else{
            return redirect('/home/register/register')->with('error','激活码失效');
        }
    }
}
